#include "stdafx.h"
#include "Grid.h"
#include "ColorDef.h"

Grid::Grid(void)
	: _bShowGrid(false)
	, _gridLen(20)
{
}


Grid::~Grid(void)
{
}


void Grid::drawGrid(CDC* pDC, CWnd* pWnd)
{
	if (_bShowGrid)
	{
		CRect wndRect;
		pWnd->GetClientRect(wndRect);
		for (int i = _gridLen; i < wndRect.Width(); i += _gridLen)
		{
			for (int j = _gridLen; j < wndRect.Height(); j += _gridLen)
			{
				pDC->SetPixel(i, j, COLOR_GRAY);
			}
		}
	}
}


void Grid::PointToGrid(CPoint& pt)
{
	if (_bShowGrid)
	{
		LongToGrid(pt.x);
		LongToGrid(pt.y);
	}
}


void Grid::LongToGrid(LONG& i)
{
	int remainder = i % _gridLen;
	int toRight   = _gridLen - remainder;

	i += toRight < remainder ? toRight : -remainder;
}

